package com.xiaodong.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaodong.reggie.currency.BaseContest;
import com.xiaodong.reggie.currency.CostomException;
import com.xiaodong.reggie.entity.*;
import com.xiaodong.reggie.mapper.AddressBookMapper;
import com.xiaodong.reggie.mapper.OrdersMapper;
import com.xiaodong.reggie.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.AntPathMatcher;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Orders> implements OrdersService {

    @Autowired
    private ShoppingCartService shoppingCartService;

    @Autowired
    private AddressBookService addressBookService;

    @Autowired
    private UserService userService;

    @Autowired
    private OrderDetallService orderDetallService;

    //下单全过程
    @Override
    public void goods(Orders orders) {
        //查询当前用户购物车中的数据
        LambdaQueryWrapper<ShoppingCart> queryWrapper=new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId, BaseContest.getId());
        List<ShoppingCart> listShoppingCart = shoppingCartService.list(queryWrapper);

        if(listShoppingCart == null || listShoppingCart.size()==0){
            throw new CostomException("没有商品不能下单");
        }
        //查询地址
        Long addressBookId = orders.getAddressBookId();
        AddressBook byId = addressBookService.getById(addressBookId);
        if(byId==null){
            throw new CostomException("用户订单有吴，不能下单");
        }

        //查询用户的信息
        User byId1 = userService.getById(BaseContest.getId());

        long orderId = IdWorker.getId();//设置订单号

        AtomicInteger amount=new AtomicInteger(0);


        List<OrderDetail> orderDetails = listShoppingCart.stream().map((item) -> {

            OrderDetail orderDetail = new OrderDetail();
            orderDetail.setOrderId(orderId);
            orderDetail.setNumber(item.getNumber());
            orderDetail.setDishFlavor(item.getDishFlavor());
            orderDetail.setDishId(item.getDishId());
            orderDetail.setSetmealId(item.getSetmealId());
            orderDetail.setName(item.getName());
            orderDetail.setImage(item.getImage());
            orderDetail.setAmount(item.getAmount());
            amount.addAndGet(item.getAmount().multiply(new BigDecimal(item.getNumber())).intValue());
            return orderDetail;

        }).collect(Collectors.toList());

        //插入数据向订单表
        orders.setOrderTime(LocalDateTime.now());
        orders.setCheckoutTime(LocalDateTime.now());
        orders.setStatus(2);
        orders.setAmount(new BigDecimal(amount.get()));//总金额
        orders.setUserId(BaseContest.getId());
        orders.setNumber(String.valueOf(orderId));
        orders.setUserName(byId1.getName());
        orders.setConsignee(byId.getConsignee());
        orders.setPhone(byId.getPhone());
        orders.setAddress((byId.getProvinceName() == null ? "" : byId.getProvinceName())
                + (byId.getCityName() == null ? "" : byId.getCityName())
                + (byId.getDistrictName() == null ? "" : byId.getDistrictName())
                + (byId.getDetail() == null ? "" : byId.getDetail()));
        this.save(orders);

        //插入数据向订单明细表
        orderDetallService.saveBatch(orderDetails);

        //清空购物车
        shoppingCartService.remove(queryWrapper);

    }
}
